package com.css.zfzx.fwsjcj.modules.quality.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.fwsjcj.modules.quality.service.QualityService;
import com.css.zfzx.fwsjcj.modules.quality.vo.QualityVo;
import com.css.zfzx.fwsjcj.modules.taskmanage.task.vo.TaskQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author leon
 * @Title: QualityController
 * @Description: 普查质量检测
 * @date 2020/6/16 10:04
 */
@RestController
@RequestMapping("/quality")
@Slf4j
public class QualityController {
    @Autowired
    private QualityService qualityService;

    /**
     * 质检任务分页
     */
    @GetMapping("task/page")
    public RestResponse getPage(HttpServletRequest request, TaskQueryVo taskQueryVo) {
        RestResponse restResponse;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = qualityService.getPage(taskQueryVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分配质检员
     *
     * @param qualityVo
     * @return
     */
    @PostMapping("task/send")
    public RestResponse sendQualityInspector(@RequestBody QualityVo qualityVo) {
        RestResponse restResponse;
        try {
            qualityService.sendQualityInspector(qualityVo);
            JSONObject object = new JSONObject();
            object.put("message", "分配质检员成功");
            restResponse = RestResponse.succeed(object);
        } catch (Exception e) {
            String errormessage = "分配质检员失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 质检分页（只包含当前登录用户）
     */
    @GetMapping("task/page/user")
    public RestResponse getPageByUser(HttpServletRequest request, TaskQueryVo taskQueryVo) {
        RestResponse restResponse;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = qualityService.getPageByUser(taskQueryVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 普查质检分页（查询一个任务下的普查列表）
     */
    @GetMapping("pc/page")
    public RestResponse getPage(HttpServletRequest request, QualityVo qualityVo) {
        RestResponse restResponse;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = qualityService.getPage(qualityVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 质检
     *
     * @param id 任务id
     * @return
     */
    @PostMapping("inspection/{id}")
    public RestResponse qualityInspection(@PathVariable String id) {
        RestResponse restResponse;
        try {
            qualityService.qualityInspection(id);
            JSONObject object = new JSONObject();
            object.put("message", "质检完毕");
            restResponse = RestResponse.succeed(object);
        } catch (Exception e) {
            String errormessage = "质检异常";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 查看任务相关的表单信息
     *
     * @return
     */
    @GetMapping("pc/findById")
    public RestResponse findById(String formId, String type) {
        RestResponse restResponse = null;
        try {
            Object object = qualityService.findHouse(type, formId);
            restResponse = RestResponse.succeed(object);
        } catch (Exception e) {
            String errormessage = "查看失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
